package com.wirelesshomecontrol.gui.windows;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.vaadin.data.util.QueryContainer;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.wirelesshomecontrol.db.jdbcconnect;
import com.wirelesshomecontrol.gui.WirelessHomeControlWindow;

public class DeleteModuleWindow extends Window{
	
	Window deleteWindow = new Window();
	private jdbcconnect cn = new jdbcconnect();
	
	
	private Button btn_del_mod = new Button("Delete Module");
	 
	 private TextField txt_nickname = new TextField("Module Name");
		
	public Table tblDataTable = null;

	public DeleteModuleWindow() {
		initDeleteWindow();
	}

	public void initDeleteWindow() {
		HorizontalLayout toolbar = new HorizontalLayout();
		deleteWindow.setCaption("Delete Sensor Modules");
		deleteWindow.center();
		deleteWindow.setWidth("30%");
		deleteWindow.setHeight("65%");
		//settingsWindow.setPositionX(100);
		//settingsWindow.setPositionY(100);
		txt_nickname.setRequired(true);
		txt_nickname.setRequiredError("You must enter a Sensor name");
		
		deleteWindow.addComponent(new Label("Type Name of Sensor to delete."));
		deleteWindow.addComponent(new Label("<br />",Label.CONTENT_XHTML));
		deleteWindow.addComponent(txt_nickname);
		deleteWindow.addComponent(btn_del_mod);
		deleteWindow.addComponent(new Label("<br />",Label.CONTENT_XHTML));
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(cn.dbUrl, "root","Hippos44");
			cn.qcSQLSensor = new QueryContainer("SELECT nickname FROM sensors", con);
			tblDataTable = new Table();
			tblDataTable.setWidth(90, Sizeable.UNITS_PERCENTAGE);
			tblDataTable.setContainerDataSource(cn.qcSQLSensor);
 
			tblDataTable.setVisible(true);
			tblDataTable.setEnabled(true);
			tblDataTable.setCaption("Current Sensors on System (Names)");
			deleteWindow.addComponent(tblDataTable);
			// cn.qcSQLHistory.close(); // Closes Query Can Not reopen

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	//}
		
			btn_del_mod.addListener(new Button.ClickListener() {
				
				
				public void buttonClick(ClickEvent event) {
					// TODO Auto-generated method stub
					String UserIn = txt_nickname.getValue() + "";
					
					String mod_delete ="'" + txt_nickname.getValue() + "'";
					String query = "DELETE FROM sensors WHERE nickname=" + mod_delete;
					try {
						
						Class.forName("com.mysql.jdbc.Driver");
						Connection con = DriverManager.getConnection (cn.dbUrl,"root","Hippos44");
						PreparedStatement stmt = con.prepareStatement(query);
						
						int rs = stmt.executeUpdate(); //returns number rows affected
						if (rs ==0){
							deleteWindow.showNotification("Sensor Not Found");
						}else {
							deleteWindow.showNotification("Sensor Removed");
							
						}
						con.close();
						} //end try

						catch(ClassNotFoundException e) {
						e.printStackTrace();
						}

						catch(SQLException e) {
						e.printStackTrace();
						}
					
				}
				
			});
		
		//btn_add_user.setIcon(new ThemeResource("icons/32/Add-male-user-32.png"));
        //btn_clr_login.setIcon(new ThemeResource("icons/32/Close-32.png"));
        //btn_clr_sensor.setIcon(new ThemeResource("icons/32/Close-32.png"));
	}

	public Window getDelete() {
		return deleteWindow;
	}

	public void setSettingsWindow(Window deleteWindow) {
		this.deleteWindow = deleteWindow;
	}
}


